﻿@page "/referencesourcelibrary/create"
@attribute [Authorize(Roles = "User, Administrator")]
@inherits OwningComponentBase<ICovenantService>

@using Covenant.Core
@using Covenant.Models.Grunts
@inject NavigationManager NavigationManager

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap pb-2 mb-3">
    <h1 class="h2">Create ReferenceSourceLibrary</h1>
</div>

<ReferenceSourceLibraryForm ReferenceSourceLibrary="ReferenceSourceLibrary" OnSubmit="OnCreate" SubmitIcon="plus" SubmitLabel="Create" />

@code {
    [Parameter]
    public ReferenceSourceLibrary ReferenceSourceLibrary { get; set; }

    protected override void OnInitialized()
    {
        this.ReferenceSourceLibrary = this.ReferenceSourceLibrary ?? new ReferenceSourceLibrary();
    }

    public async Task OnCreate(ReferenceSourceLibrary library)
    {
        try
        {
            this.ReferenceSourceLibrary = await Service.CreateReferenceSourceLibrary(library);
            NavigationManager.NavigateTo("/grunttask");
        }
        catch (Exception e) when (e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException)
        {
            // return RedirectToAction(nameof(Edit), new { id = libraryModel.Id });
        }
    }
}